#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: website.py
@contact: zwb15083976291@163.com
@time: 2021/12/07
"""
from django.utils.decorators import method_decorator

from admin.mixins import AdminLoginMixin
from admin.serializers.website import WebInfoSerializer, UserSerializer, UpdatePasswordSerializer
from common.models import BlogWebInfo, BlogUser
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.user_authentication import allow_admin_user
from utils.views.views import BaseGenericAPIView


@method_decorator(allow_admin_user, name='dispatch')
class WebSiteConfigureView(BaseGenericAPIView):
    serializer_class = WebInfoSerializer
    diff_methods_request_params = dict(get=dict(optional=["id"]),
                                       put=dict(required=['id', 'notice', 'website_title', 'website_records']))

    def get(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.ACCESS_DENIED, message=msg)
        return CommonResponse(data=WebInfoSerializer(BlogWebInfo.objects.all()[0]).data)

    def put(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        self.serializer.select_field_update_obj()
        return CommonResponse(data=self.serializer.data)


@method_decorator(allow_admin_user, name='dispatch')
class AboutMeConfigureView(AdminLoginMixin, BaseGenericAPIView):
    serializer_class = UserSerializer
    diff_methods_request_params = dict(get=dict(optional=["id"]),
                                       put=dict(required=['id', 'user_image_url', 'email', 'github_link', 'gitee_link',
                                                          'blog_name', 'motto', 'qq_account', 'about']))

    def get(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        return CommonResponse(data=UserSerializer(BlogUser.objects.all()[0]).data)

    def put(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        self.serializer.select_field_update_obj()
        return CommonResponse(data=self.serializer.data)


@method_decorator(allow_admin_user, name='dispatch')
class UpdatePasswordView(AdminLoginMixin, BaseGenericAPIView):
    serializer_class = UpdatePasswordSerializer
    diff_methods_request_params = dict(put=dict(required=['id', 'password', 'new_password', 'confirm_password']))

    def put(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=msg)
        # 更新密码
        self.update_password()
        self.login_out()
        return CommonResponse()

    def update_password(self):
        new_password = self.serializer.validated_data.get('new_password')
        user = self.serializer.context['user']
        user.password = new_password
        user.save()
